package com.hmdp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RedisConstant;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Override
    public Result isFollow(Long followUserId) {
        //获取当前登录的userId
        Long userId = UserHolder.getUser().getId();
        LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<>();
        //查询当前用户是否关注了该笔记的博主
        queryWrapper.eq(Follow::getUserId, userId).eq(Follow::getFollowUserId, followUserId);
        //只查询一个count就行了
        int count = this.count(queryWrapper);
        return Result.ok(count > 0);
    }

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private IUserService userService;

    @Override
    public void follow(Long followUserId, Boolean isFellow) {
        //获取当前用户id
        Long userId = UserHolder.getUser().getId();
        String key = RedisConstant.Follow + userId;
        //判断是否关注
        if (isFellow) {
            //关注，则将信息保存到数据库
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            boolean save = save(follow);
            if (!save) return;
            stringRedisTemplate.opsForSet().add(key, String.valueOf(followUserId));
        } else {
            //取关，则将数据从数据库中移除
            LambdaQueryWrapper<Follow> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Follow::getUserId, userId).eq(Follow::getFollowUserId, followUserId);
            boolean remove = remove(queryWrapper);
            if (!remove) return;
            stringRedisTemplate.opsForSet().remove(key, String.valueOf(followUserId));
        }
    }

    /**
     * 共同关注
     *
     * @param id 对方的id
     */
    @Override
    public List<UserDTO> gtFollow(Integer id) {
        Long me_id = UserHolder.getUser().getId();
        String me_id_key = RedisConstant.Follow + me_id;
        String id_key = RedisConstant.Follow + id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(me_id_key, id_key);
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        //   无交集

        if (ids.isEmpty() || ids == null) return Collections.emptyList();

        List<User> users = userService.listByIds(ids);
        List<UserDTO> collect = users.stream().map(item -> {

            UserDTO userDTO = new UserDTO();

            BeanUtils.copyProperties(item, userDTO);
            return userDTO;
        }).collect(Collectors.toList());

        return collect;
    }
}
